package com.ruoyi.scm.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.scm.domain.ScmPurchaseApply;
import com.ruoyi.scm.domain.ScmPurchaseApplyItem;
import com.ruoyi.scm.mapper.ScmPurchaseApplyMapper;
import com.ruoyi.scm.mapper.ScmPurchasePlanMapper;
import com.ruoyi.scm.service.IScmPurchaseApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 采购申请Service业务层处理
 *
 * @author Ryan
 * @date 2025-03-12
 */
@Service
public class ScmPurchaseApplyServiceImpl implements IScmPurchaseApplyService
{
    @Autowired
    private ScmPurchaseApplyMapper scmPurchaseApplyMapper;

    @Autowired
    private ScmPurchasePlanMapper scmPurchasePlanMapper;

    /**
     * 查询采购申请
     *
     * @param id 采购申请主键
     * @return 采购申请
     */
    @Override
    public ScmPurchaseApply getById(Long id)
    {
        return scmPurchaseApplyMapper.getById(id);
    }

    /**
     * 查询采购申请列表
     *
     * @param scmPurchaseApply 采购申请
     * @return 采购申请
     */
    @Override
    public List<ScmPurchaseApply> getList(ScmPurchaseApply scmPurchaseApply)
    {
        return scmPurchaseApplyMapper.getList(scmPurchaseApply);
    }

    /**
     * 新增采购申请
     *
     * @param scmPurchaseApply 采购申请
     * @return 结果
     */
    @Override
    public int add(ScmPurchaseApply scmPurchaseApply)
    {
        scmPurchaseApply.setApplicationNo(generateNumber("CGSQ"));
        scmPurchaseApply.setApplicantId(SecurityUtils.getUserId());
        scmPurchaseApply.setApplicantDate(DateUtils.getNowDate());
        int row = scmPurchaseApplyMapper.add(scmPurchaseApply);
        insertItem(scmPurchaseApply);
        return row;
    }

    /**
     * 修改采购申请
     *
     * @param scmPurchaseApply 采购申请
     * @return 结果
     */
    @Override
    public int edit(ScmPurchaseApply scmPurchaseApply)
    {
        scmPurchaseApplyMapper.delItem(scmPurchaseApply.getId());
        insertItem(scmPurchaseApply);
        return scmPurchaseApplyMapper.edit(scmPurchaseApply);
    }

    /**
     * 批量删除采购申请
     *
     * @param ids 需要删除的采购申请主键
     * @return 结果
     */
    @Override
    public int del(Long[] ids)
    {
        for (Long id : ids) {
            scmPurchaseApplyMapper.delItem(id);
        }
        return scmPurchaseApplyMapper.del(ids);
    }


    public void insertItem(ScmPurchaseApply scmPurchaseApply){
        List<ScmPurchaseApplyItem> scmPurchaseApplyItemList = scmPurchaseApply.getScmPurchaseApplyItemList();

        if (!scmPurchaseApplyItemList.isEmpty()) {
            for (ScmPurchaseApplyItem scmPurchaseApplyItem : scmPurchaseApplyItemList) {
                scmPurchaseApplyItem.setPurchasingApplicationId(scmPurchaseApply.getId());
            }

            scmPurchaseApplyMapper.addItem(scmPurchaseApplyItemList);
        }
    }

    public String generateNumber(String number){
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return number+format.format(date);
    }
}
